<?php
namespace App\Service\H5;

use App\Service\BaseService as Service;

class IndexService extends Service
{
    //获取页面
    public static function index($code)
    {
        $qrOne = self::dbConn(self::$dbQr)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        if (!$qrOne) {
            return self::toback(-2, '这是空码');
        }
        if (!$qrOne->yw_type || !$qrOne->yw_id) {
            return self::toback(-2, '这是空码');
        }
        $ary = [
            'code' => $qrOne->code,
            'type' => $qrOne->yw_type,
            'id' => $qrOne->yw_id,
        ];
        //h5code
        $h5code = toCode();
        self::dbConn(self::$dbQrlog)->insertGetId([
            'h5code' => $h5code, 'code' => $ary['code'],
            'yw_type' => $ary['type'], 'yw_id' => $ary['id'],
            'addtime' => toDate(),
        ]);
        $ary['h5code'] = $h5code;
        return self::toback(0, '', $ary);
    }

    //详情
    public static function one($code)
    {
        $qrOne = self::dbConn(self::$dbQr)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        if (!$qrOne) {
            return self::toback(-2, '这是空码');
        }
        $type = $qrOne->yw_type;
        $ary = [];
        //个人码
        if ($type == 1) {
            $ary = self::tmpOne($code);
        }
        //商品码
        else if ($type == 2) {
            $res = self::dbConn(self::$dbYwGoods)
                ->where('del', 0)
                ->where('code', $code)
                ->first();
        }
        else if ($type == 3) {
            $res = self::dbConn(self::$dbYwFuwu)
                ->where('del', 0)
                ->where('code', $code)
                ->first();
        }
        else {
            return self::toback(-2, '码类型错误');
        }
        if (!$ary) {
            return self::toback(-2, '没有内容');
        }
        $ary['code'] = $code;
        $ary['show_code'] = $qrOne->show_code;
        $ary['visit_total'] = self::dbConn(self::$dbQrlog)
            ->where('code', $code)
            ->count();
        $adOne = self::dbConn(self::$dbAd)
            ->select('id', 'thumb', 'link')
            ->where('del', 0)
            ->where('isrun', 2)
            ->first();
        $ary['ad'] = '';
        if (!empty($adOne)) {
            $ary['ad'] = [
                'id' => $adOne->id, 'link' => $adOne->link,
                'cover' => file_exists($adOne->thumb) ? '/' . $adOne->thumb : '',
            ];
        }
        return self::toback(0, '', $ary);
    }

    //个人码
    public static function tmpOne($code)
    {
        $res = self::dbConn(self::$dbYwTmp)
            ->where('del', 0)
            ->where('code', $code)
            ->first();
        if (empty($res)) {
            return [];
        }
        $cateOne = self::dbConn(self::$dbCate)
            ->where('del', 0)
            ->where('id', $res->cate_id)
            ->first();
        $cate_name = $cateOne ? $cateOne->title : '-';
        $attrs = self::dbConn(self::$dbAttr)
            ->select('id', 'attr_name', 'attr_val')
            ->where('del', 0)
            ->where('yw_type', 1)
            ->where('yw_id', $res->id)
            ->get();
        return [
            'type' => 1, 'id' => $res->id, 'title' => $res->title,
            'cate_id' => $res->cate_id, 'cate_name' => $cate_name,
            'cover' => file_exists($res->thumb) ? '/' . $res->thumb : '',
            'detail' => $res->describe, 'attrs' => $attrs,
        ];
    }

    //广告已读设置
    public static function adRead($ad_id, $h5code, $type, $tmpid)
    {
        $adOne = self::dbConn(self::$dbAd)
            ->where('del', 0)
            ->where('isrun', 2)
            ->where('id', $ad_id)
            ->first();
        if (empty($adOne)) {
            return self::toback(-2, '不在可用状态');
        }
        if (!$adOne->num) {
            return self::toback(-2, '没有可展示次数');
        }
        if ($type == 1) {
            $ywOne = self::dbConn(self::$dbYwTmp)
                ->where('id', $tmpid)
                ->first();
        } else {
            return self::toback(-2, '业务数据错误');
        }
        $view_uid = !empty($ywOne) ? $ywOne->id : 0;
        self::dbConn(self::$dbAdUsed)->insertGetId([
            'ad_id' => $ad_id, 'pub_uid' => $adOne->uid,
            'view_uid' => $view_uid, 'h5code' => $h5code,
            'addtime' => toDate(),
        ]);
        self::dbConn(self::$dbAd)
            ->where('id', $ad_id)
            ->update(['num' => $adOne->num - 1, 'uptime' => toDate()]);
        return self::toback();
    }
}